package com.yuan.spring.springUtil;

import org.junit.Test;
import org.springframework.util.DigestUtils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by Administrator on 2017/9/27.
 */
public class MD5 {


    public static String aaMd5(String a) throws NoSuchAlgorithmException {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        byte[] bytes = messageDigest.digest(a.getBytes());
        System.out.println("通过bigint转化");
        System.out.println(new BigInteger(1, bytes).toString(16));
        //手动进行转化把 8 位 16 字节   ->  转化成16位 32 字节
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(Integer.toHexString(b >>> 4 & 0xf));
            sb.append(Integer.toHexString(b & 0xf));
        }
        System.out.println("手动转化");
        System.out.println(sb.toString());
        sb.setLength(0);
        System.out.println("脱离Integer");
        String hex16 = "0123456789abcdef";
        String[] hex16s = hex16.split("");
        for (byte b : bytes) {
            sb.append(hex16s[b >>> 4 & 0xf]);
            sb.append(hex16s[b & 0xf]);
        }
        System.out.println(sb.toString());
        sb.setLength(0);
        System.out.println(255);
        for (int i = 0; i < bytes.length; i++) {
            if (Integer.toHexString(0xFF & bytes[i]).length() == 1) {
                sb.append("0").append(Integer.toHexString(0xFF & bytes[i]));
            } else {
                sb.append(Integer.toHexString(0xFF & bytes[i]));
            }
        }


        return sb.toString();
    }

    /**
     * 对字符串md5加密(小写+字母)
     *
     * @param str 传入要加密的字符串
     * @return MD5加密后的字符串
     */
    public static String getMD5(String str) {
        try {
            // 生成一个MD5加密计算摘要
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 计算md5函数
            md.update(str.getBytes());
            // digest()最后确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
            // BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
            return new BigInteger(1, md.digest()).toString(16);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String MD5(String str) {
        String outStr = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(str.getBytes());
            byte b[] = md.digest();
            StringBuffer buf = new StringBuffer();
            for (int i = 0; i < b.length; i++) {
                int c = b[i] >>> 4 & 0xf;
                buf.append(Integer.toHexString(c));
                c = b[i] & 0xf;
                buf.append(Integer.toHexString(c));
            }
            outStr = buf.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return outStr;
    }

    @Test
    public void a() throws Exception {
        String a = "123456";
        System.out.println(DigestUtils.md5DigestAsHex(a.getBytes()));
        System.out.println(MD5(a));
        System.out.println(getMD5(a));
        System.out.println(aaMd5(a));

    }
}
